
package com.ttomato.game.glbasics;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;

import com.ttomato.game.framework.Game;
import com.ttomato.game.framework.Screen;
import com.ttomato.game.framework.impl.GLGame;
import com.ttomato.game.framework.impl.GLGraphics;

public class FirstTriangleTest extends GLGame {

    @Override
    public Screen getStartScreen() {
        return new FirstTriangleScreen(this);
    }

    class FirstTriangleScreen extends Screen {
        GLGraphics mGlGraphics;
        FloatBuffer mVertices;
        final static int VERTEX_SIZE = (2 + 4) * 4;

        public FirstTriangleScreen(Game game) {
            super(game);

            mGlGraphics = ((GLGame) game).getGLGraphics();

            ByteBuffer byteBuffer = ByteBuffer.allocateDirect(3 * VERTEX_SIZE);
            byteBuffer.order(ByteOrder.nativeOrder());
            mVertices = byteBuffer.asFloatBuffer();

            mVertices.put(new float[] {
                    0.0f, 0.0f, 1f, 0f, 0f, 1f,
                    319.0f, 0.0f, 0f, 1f, 0f, 1f,
                    160.0f, 479.0f, 0f, 0f, 1f, 1f
            });

            mVertices.flip();
        }

        @Override
        public void update(float deltaTime) {
            mGame.getInput().getTouchEvents();
            mGame.getInput().getKeyEvents();
        }

        @Override
        public void present(float deltaTime) {
            GL10 gl = mGlGraphics.getGl();

            gl.glViewport(0, 0, mGlGraphics.getWidth(), mGlGraphics.getHeight());
            gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
            gl.glMatrixMode(GL10.GL_PROJECTION);
            gl.glLoadIdentity();
            gl.glOrthof(0, 320, 0, 480, 1, -1);

            gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
            gl.glEnableClientState(GL10.GL_COLOR_ARRAY);

            mVertices.position(0);
            gl.glVertexPointer(2, GL10.GL_FLOAT, VERTEX_SIZE, mVertices);
            mVertices.position(2);
            gl.glColorPointer(4, GL10.GL_FLOAT, VERTEX_SIZE, mVertices);

            gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);
        }

        @Override
        public void pause() {

        }

        @Override
        public void resume() {

        }

        @Override
        public void dispose() {

        }

    }

}
